/*
* @(#)ListFormGroupRecordAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.form.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.form.bean.FormGroup;
import com.dcivision.form.bean.FormRecord;
import com.dcivision.form.dao.FormGroupDAObject;
import com.dcivision.form.dao.FormRecordDAObject;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.web.AbstractListAction;

/**
 * <p>Class Name:       ListFormGroupRecordAction.java    </p>
 * <p>Description:      The list action class for ListFormGroupRecordAction.jsp</p>
 *
 * @author           Tony Chen
 * @company          DCIVision Limited
 * @creation date    19/02/2003
 * @version          $Revision: 1.8 $
 */

public class ListFormGroupRecordAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.8 $";

  public static final String VAR_FORM_RECORD_LIST = "listFormRecordList";
  public static final String VAR_ACTIVE_RECORD_LIST = "listActiveRecord";


  /** Creates a new instance of ListFormGroupRecordAction */
  public ListFormGroupRecordAction() {
    super();
    this.setListName("formGroupList");
    this.setListFunctionName("getAccessibleList");
  }

  /** getFunctionCode
   *
   * Abstract function which sub-class should implement to return the corresponding
   * function code.
   *
   * @return   The function code
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.FORM_SETUP);
  }

  /** getMajorDAOClassName
   *
   * Abstract function which sub-class should implement to return the corresponding
   * major DAO class name used in this class.
   *
   * @return   The DAO class name
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.form.dao.FormGroupDAObject");
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    String function = request.getParameter("function");
    request.setAttribute("function", function);

    if (function.startsWith("FORM_MONITOR")) {
      super.setFunctionCode(SystemFunctionConstant.FORM_MONITOR);
      ((ListFormGroupRecordForm)form).setListType("M");
      this.setListFunctionName("getAccessibleMonitoringReportList");
    } else if(function.startsWith("FORM_REPORT")) {
      super.setFunctionCode(SystemFunctionConstant.FORM_REPORT);
      ((ListFormGroupRecordForm)form).setListType("G");
      this.setListFunctionName("getAccessibleMonitoringReportList");
    } else {
      super.setFunctionCode(SystemFunctionConstant.FORM_SETUP);
      ((ListFormGroupRecordForm)form).setListType(null);
      this.setListFunctionName("getAccessibleList");
    }

    ActionForward actionForward = super.execute(mapping,form,request,response);

    FormGroupDAObject dao = (FormGroupDAObject) this.getMajorDAObject(request);
    FormRecordDAObject recordDAO = (FormRecordDAObject) this.getDAObjectByClassName(request, "com.dcivision.form.dao.FormRecordDAObject");

    List lastFormRecordList = new ArrayList();
    List noOfActiveRecordList = new ArrayList();
    List resultList = (List)request.getAttribute(this.m_sListName);

    for (int i=0; i<resultList.size(); i++) {
      FormGroup formGroup = (FormGroup) resultList.get(i);
      Integer activeRecordCount = null;
      if (function.startsWith("FORM_MONITOR")) {
        activeRecordCount = recordDAO.getNoOfActiveRecordForMonitoringByFormGroupID(formGroup.getID());
      } else if(function.startsWith("FORM_REPORT")) {
        activeRecordCount = recordDAO.getNoOfActiveRecordForReportByFormGroupID(formGroup.getID());
      } else {
        activeRecordCount = recordDAO.getNoOfActiveRecordByFormGroupID(formGroup.getID());
      }
      noOfActiveRecordList.add(activeRecordCount);
      if (activeRecordCount.intValue()>0) {
        FormRecord lastFormRecord = (FormRecord)recordDAO.getLastUpdateFormRecordByFormGroupID(formGroup.getID());
        lastFormRecordList.add(lastFormRecord);
      } else {
        lastFormRecordList.add(new FormRecord());
      }
    }
    request.setAttribute(VAR_ACTIVE_RECORD_LIST, noOfActiveRecordList);
    request.setAttribute(VAR_FORM_RECORD_LIST, lastFormRecordList);

    return(actionForward);
  }
}
